package com.easou.ps.dk.services;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.easou.ps.dk.domain.ABaiduResult;
import com.easou.ps.dk.domain.BaiduResult;
import com.easou.ps.dk.domain.QueryInfo;
import com.easou.ps.dk.domain.QueryParams;
import com.easou.ps.dk.domain.ResultInfo;
import com.easou.ps.dk.utils.Configer;
import com.easou.ps.dk.utils.GetDay;
import com.easou.ps.dk.utils.RegexHelper;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class Spider {
	public static final Logger LOG_DK = Logger.getLogger("dk");
	private static WebClient webClient = new WebClient();

	@SuppressWarnings("unchecked")
	public QueryInfo pass(int index, String query, QueryParams qp, String wver) {
		webClient.setJavaScriptEnabled(false);
		QueryInfo queryInfo = new QueryInfo();
		queryInfo.setQuery(query);
		queryInfo.setCorrectQuery("");

		query = replace(query);
		List<ResultInfo> resultInfos = new ArrayList<ResultInfo>();
		String url = "";
		try {
			Set<String> baiduUrls = getBaiduUrlsByName(qp, query);
			
			url = "http://" + Configer.get("host") + "/s.m?q=" + URLEncoder.encode(query, "UTF-8") + "&sty=1&c=6&fr=14.0.10.111&cid=&chn_i=%E6%90%9C%E7%B4%A2&esid=LJmaHUBlUQd&wver=" + wver + "&w=1&debug=1&clearCache=1";
			LOG_DK.info("query:"+query+",URL:" + url);
			HtmlPage page = webClient.getPage(url);
			List<HtmlElement> elements = (List<HtmlElement>) page.getByXPath("//div[@class=\"item\"]");
			for (int i = 0; i < elements.size(); i++) {
				String aResultInfo = elements.get(i).asText();
				String theUrl = RegexHelper.getValueByPattern(aResultInfo, "titleURL:(.*)");
				String score = RegexHelper.getValueByPattern(aResultInfo, "score:(.*?);");
				ResultInfo resultInfo = new ResultInfo();
				resultInfo.setOriginalUrl(theUrl);
				resultInfos.add(resultInfo);
				LOG_DK.info(query + "_" + i + ":" + theUrl);
				
				if(baiduUrls.contains(theUrl)){
					baiduUrls.remove(theUrl);
					qp.getBaiduResult().getABaidu(query).getUrls().put(theUrl, i+"_"+score);
					qp.addCount();
				}
			}
			
			//保存网页
			String dataPath = Configer.get("data_path");
			dataPath = (dataPath.endsWith("/") ? dataPath.substring(0, dataPath.length()-1) : dataPath);
			File todayPath = new File(dataPath+"/"+GetDay.getToday());
			if(!todayPath.exists()){
				todayPath.mkdir();
			}
			String fileName = dataPath + "/" + GetDay.getToday() + "/" + index + "_" + query + ".html";
			System.out.println("save : " + fileName);
			File file = new File(fileName);
			FileOutputStream fos = new FileOutputStream(file);
			byte[] bytes = page.asXml().getBytes();
			fos.write(bytes);
			fos.close();
			
			//翻页查询
			int pageNo = 0;
			boolean searchMore = baiduUrls.isEmpty();
			while(!searchMore && pageNo++<10){	
				HtmlElement firstByXPath = page.getFirstByXPath("/html/body/div/div[4]/div[12]/div/table/tbody/tr/td[3]/a");
				if(firstByXPath == null){
					searchMore = false;
					continue;
				}else{
					HtmlPage morePage = firstByXPath.click();
					List<HtmlElement> moreElements = (List<HtmlElement>) morePage.getByXPath("//div[@class=\"item\"]");
					for (int i = 0; i < moreElements.size(); i++) {
						String aResultInfo = moreElements.get(i).asText();
						String theUrl = RegexHelper.getValueByPattern(aResultInfo, "titleURL:(.*)");
						String score = RegexHelper.getValueByPattern(aResultInfo, "score:(.*?);");
						ResultInfo resultInfo = new ResultInfo();
						resultInfo.setOriginalUrl(theUrl);
						resultInfos.add(resultInfo);
						LOG_DK.info(query + "_" + i + ":" + theUrl);
						
						if(baiduUrls.contains(theUrl)){
							baiduUrls.remove(theUrl);
							qp.getBaiduResult().getABaidu(query).getUrls().put(theUrl, i+"_"+score);
							qp.addCount();
							searchMore = !baiduUrls.isEmpty();
						}
					}
				}
			}
			
			System.out.println(query +" finish: "+ (10-baiduUrls.size()));
			
			queryInfo.setResultInfos(resultInfos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return queryInfo;
	}
	
	private Set<String> getBaiduUrlsByName(QueryParams qp, String query){
		ABaiduResult baiduResult = qp.getBaiduResult().getABaidu(query);
		Map<String, String> baiduUrls = baiduResult.getUrls();
		Set<String> baiduUrlSet = baiduUrls.keySet();
		return baiduUrlSet;
	}

	public static String replace(String src) {
		String result = src;
		result = result.replaceAll(":", "：");
		result = result.replaceAll("\\?", "？");
		result = result.replaceAll("\\<", "《");
		result = result.replaceAll("\\>", "》");
		return result;
	}
}
